package fr.umlv.galcon.ship.type;

import java.awt.Point;
import java.awt.geom.Point2D;

import fr.umlv.galcon.item.GraphicItem;
import fr.umlv.galcon.ship.SendShip;
import fr.umlv.galcon.ship.ShipAbstract;
import fr.umlv.galcon.ship.ShipProperties;
import fr.umlv.galcon.ship.Squadron;

/**
 * Abstract class for triangular Ships.
 * 
 */
public abstract class TriangularShip extends ShipAbstract {
    private final Point2D[] vertices;

    /**
     * Constructor
     * 
     * @param x
     *            X coordinate of the Ship.
     * @param y
     *            Y coordinate of the Ship.
     * @param width
     *            Width of the Ship.
     * @param squadron
     *            Squadron which contains the Ship.
     */
    protected TriangularShip(int x, int y, int width, ShipProperties shipProperties, Squadron squadron) {
	super(x, y, width, shipProperties, squadron);
	this.vertices = new Point[3];
    }

    @Override
    public boolean contains(Point2D p) {
	double currentX = super.getCenter().getX();
	double currentY = super.getCenter().getY();

	double rayon = super.getWidth() / 2;
	double distance = (p.getX() - currentX) * (p.getX() - currentX) + (p.getY() - currentY) * (p.getY() - currentY);
	return (distance <= rayon * rayon);
    }

    /**
     * Get Ship's X vertices array
     * 
     * @return X vertices array
     */
    public int[] getXVertices() {
	int[] x = new int[3];
	int i = 0;
	for (Point2D p : this.vertices) {
	    x[i] = (int) p.getX();
	    i++;
	}
	return x;
    }

    /**
     * Get Ship's Y vertices array
     * 
     * @return Y vertices array
     */
    public int[] getYVertices() {
	int[] y = new int[3];
	int i = 0;
	for (Point2D p : this.vertices) {
	    y[i] = (int) p.getY();
	    i++;
	}
	return y;
    }

    /**
     * Change the orientation of the Ship.
     */
    public void orientationShip() {
	double currentX = super.getCenter().getX();
	double currentY = super.getCenter().getY();
	int width = super.getWidth();

	double theta = SendShip.getAngle(getShipMovement().getLastPosition(), getCenter());

	this.vertices[0] = new Point((int) (currentX + width / 2), (int) (currentY));
	this.vertices[1] = new Point((int) (currentX - width / 2), (int) (currentY - width / 2));
	this.vertices[2] = new Point((int) (currentX - width / 2), (int) (currentY + width / 2));

	double x, y, xIni, yIni;

	for (int i = 0; i < this.vertices.length; i++) {
	    xIni = this.vertices[i].getX();
	    yIni = this.vertices[i].getY();

	    x = Math.cos(-theta) * (xIni - currentX) + Math.sin(-theta) * (yIni - currentY) + currentX;
	    y = Math.cos(-theta) * (yIni - currentY) - Math.sin(-theta) * (xIni - currentX) + currentY;

	    this.vertices[i].setLocation(x, y);
	}
    }

    @Override
    public boolean intersect(GraphicItem item) {
	double radius = super.getWidth();
	return Point2D.distanceSq(item.getShape().getBounds().getCenterX(), item.getShape().getBounds().getCenterY(), super
		.getCenter().getX(), super.getCenter().getY()) <= radius * radius + radius * radius;
    }

}
